"""电影应用模型模块

定义了电影和用户电影偏好相关的数据模型。
"""

import datetime

from django.conf import settings
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models


class Movie(models.Model):
    """电影模型类"""

    title = models.CharField(max_length=255, verbose_name="电影标题")  # 电影标题字段
    genres = models.JSONField(default=list, verbose_name="电影类型")  # 电影类型字段，存储为JSON格式
    country = models.CharField(max_length=100, blank=True, null=True, verbose_name="国家")  # 国家字段
    extra_data = models.JSONField(default=dict, verbose_name="额外数据")  # 额外数据字段
    release_year = models.IntegerField(
        validators=[
            MinValueValidator(1888),
            MaxValueValidator(datetime.datetime.now().year),
        ],
        blank=True,
        null=True,
        verbose_name="发布年份",
    )  # 发布年份字段

    class Meta:
        unique_together = ("title", "country", "release_year")
        verbose_name = "电影"
        verbose_name_plural = "电影"

    def __str__(self) -> str:
        """返回电影标题"""
        return self.title


class UserMoviePreferences(models.Model):
    """用户电影偏好模型类"""

    user = models.OneToOneField(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name="movie_preferences",
        verbose_name="用户",
    )  # 关联用户的一对一字段
    preferences = models.JSONField(
        default=dict,
        help_text="Stores user preferences for movies like genres, directors, etc.",
        verbose_name="偏好设置",
    )  # 用户偏好设置字段
    watch_history = models.JSONField(
        default=list, help_text="Stores information about movies the user has watched.", verbose_name="观看历史"
    )  # 用户观看历史字段

    class Meta:
        verbose_name = "用户电影偏好"
        verbose_name_plural = "用户电影偏好"

    def __str__(self) -> str:
        """返回用户电影偏好的字符串表示"""
        return f"{self.user.username}'s Movie Preferences"
